﻿using Orther.Model.param;
using Orther.Service.Miyao;
using Project.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.ManageUser;
using VOL.Entity.DomainModels;

namespace Orther.BLL.Miyao
{
    public class BxgsBLL
    {
        private BxgsService bxgsService = new BxgsService();
        public TData<List<qx_bxgs>> GetPageList(BxgsParam param, Pagination pagination)
        {
            TData<List<qx_bxgs>> obj = new TData<List<qx_bxgs>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = bxgsService.GetPageList(param, pagination);
            obj.Tag = 1;
            obj.Total = pagination.TotalCount;
            return obj;
        }

        public async Task<TData<List<qx_bxgs>>> GetList(BxgsParam param, int? topNum)
        {
            TData<List<qx_bxgs>> obj = new TData<List<qx_bxgs>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = await bxgsService.GetList(param, topNum);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<qx_bxgs>> GetObject(BxgsParam param)
        {
            TData<qx_bxgs> obj = new TData<qx_bxgs>();
            obj.Data = await bxgsService.GetObject(param);
            obj.Tag = 1;
            return obj;
        }


        public async Task<TData> SaveForm(qx_bxgs entity)
        {
            TData obj = new TData();
            if (entity == null)
            {
                obj.Message = "qx_bxgs对象为空！";
                return obj;
            }

            if (entity.mingcheng == null)
            {
                obj.Message = "操作失败，保险公司名称不能为空！";
                return obj;
            }

            BxgsParam paramCheckCar = new BxgsParam();
            paramCheckCar.mingcheng = entity.mingcheng;
            paramCheckCar.NotEqualId = entity.Id;
            List<qx_bxgs> checkCarList = await bxgsService.GetList(paramCheckCar, null);
            if (checkCarList.Count > 0)
            {
                obj.Message = "保险公司名称：【" + entity.mingcheng + "】已经存在，不能重复添加！";
                return obj;
            }

            //新增操作
            if (entity.Id == Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                //初始化主键值
                entity.Id = Guid.NewGuid();
                entity.IsDeleted = 0;
                //初始化字段的默认值
                List<object[]> defaultCols = new List<object[]>();
                defaultCols.Add(new object[] { "CreateDate", DateTime.Now });
                defaultCols.Add(new object[] { "CreateID", UserContext.Current.UserId });
                defaultCols.Add(new object[] { "Creator", UserContext.Current.UserName });
                operatelog oLog = new operatelog();
                oLog.Log_Id = Guid.NewGuid();
                oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + defaultCols[2][1] + "添加了一条保险公司资料，保险公司名称为:【" + entity.mingcheng + "】。";
                oLog.Category = "添加";
                oLog.MenuModel = "保险公司资料";
                oLog.CreateDate = DateTime.Now;
                oLog.CreateID = UserContext.Current.UserId;
                oLog.Creator = UserContext.Current.UserName;
                bxgsService.SaveForm(entity, oLog, defaultCols);
            }
            //更新操作
            else
            {
                List<object[]> defaultCols = new List<object[]>();
                operatelog oLog = new operatelog();
                oLog.Log_Id = Guid.NewGuid();
                oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + UserContext.Current.UserName + "更新了一条保险公司资料，" +
                    "保险公司名称变更为:【" + entity.mingcheng + "】。" +
                    "负责人设置为:【" + entity.fuzeren + "】" +
                    "地址设置为:【" + entity.dizhi + "】";
                oLog.Category = "修改";
                oLog.MenuModel = "保险公司资料";
                oLog.CreateDate = DateTime.Now;
                oLog.CreateID = UserContext.Current.UserId;
                oLog.Creator = UserContext.Current.UserName;
                bxgsService.UpdateForm(entity, oLog, defaultCols);
            }


            obj.Tag = 1;

            return obj;
        }


        public async Task<TData> deleteFrom(Guid id)
        {
            TData obj = new TData();
            //第一步：获取要删除的企业名称
            BxgsParam param = new BxgsParam();
            param.Id = id;
            qx_bxgs entity = await bxgsService.GetObject(param);
            entity.IsDeleted = 1;
            //第二步：日志信息操作
            operatelog oLog = new operatelog();
            oLog.Log_Id = Guid.NewGuid();
            oLog.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + UserContext.Current.UserName + "删除了一条保险公司资料，保险公司名称为：【" + entity.mingcheng + "】。";
            oLog.Category = "删除";
            oLog.MenuModel = "保险公司资料";
            oLog.CreateDate = DateTime.Now;
            oLog.CreateID = UserContext.Current.UserId;
            oLog.Creator = UserContext.Current.UserName;
            bxgsService.UpdateForm(entity, oLog);
            obj.Tag = 1;
            return obj;
        }

    }
}
